/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.test.dqsy.alk.web;

import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.test.dqsy.alk.entity.AtLeftTree;
import com.jeeplus.modules.test.dqsy.alk.service.AtLeftTreeService;

/**
 * alkController
 * @author zty
 * @version 2020-09-17
 */
@RestController
@RequestMapping(value = "/dqsy/alk/atLeftTree")
public class AtLeftTreeController extends BaseController {

	@Autowired
	private AtLeftTreeService atLeftTreeService;

	@ModelAttribute
	public AtLeftTree get(@RequestParam(required=false) String id) {
		AtLeftTree entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = atLeftTreeService.get(id);
		}
		if (entity == null){
			entity = new AtLeftTree();
		}
		return entity;
	}


	/**
	 * alk树表数据
	 */
	@GetMapping("list")
	public AjaxJson list(AtLeftTree atLeftTree) {
		return AjaxJson.success().put("list", atLeftTreeService.findList(atLeftTree));
	}

	/**
	 * 根据Id获取alk数据
	 */
	@GetMapping("queryById")
	public AjaxJson queryById(AtLeftTree atLeftTree) {
		return AjaxJson.success().put("atLeftTree", atLeftTree);
	}

	/**
	 * 保存alk
	 */
	@PostMapping("save")
	public AjaxJson save(AtLeftTree atLeftTree, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(atLeftTree);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		atLeftTreeService.save(atLeftTree);//保存
		return AjaxJson.success("保存alk成功");
	}

	/**
	 * 删除alk
	 */
	@DeleteMapping("delete")
	public AjaxJson delete(AtLeftTree atLeftTree) {
		atLeftTreeService.delete(atLeftTree);
		return AjaxJson.success("删除alk成功");
	}

	/**
	     * 获取JSON树形数据。
	     * @param extId 排除的ID
	     * @return
	*/
	@RequiresPermissions("user")
	@GetMapping("treeData")
	public AjaxJson treeData(@RequestParam(required = false) String extId) {
		List<AtLeftTree> list = atLeftTreeService.findList(new AtLeftTree());
		List rootTree = getAtLeftTreeTree(list, extId);
		return AjaxJson.success().put("treeData", rootTree);
	}

	private List<AtLeftTree> getAtLeftTreeTree(List<AtLeftTree> list, String extId) {
		List<AtLeftTree> atLeftTrees = Lists.newArrayList();
		List<AtLeftTree> rootTrees = atLeftTreeService.getChildren("0");
		for (AtLeftTree root : rootTrees) {
		    if (StringUtils.isBlank(extId) ||  !extId.equals(root.getId())) {
		        atLeftTrees.add(getChildOfTree(root, list, extId));
		    }
		}
		return atLeftTrees;
	}

	private AtLeftTree getChildOfTree(AtLeftTree atLeftTree, List<AtLeftTree> atLeftTreeList, String extId) {
		atLeftTree.setChildren(Lists.newArrayList());
		for (AtLeftTree child : atLeftTreeList) {
		    if (StringUtils.isBlank(extId) ||  (!extId.equals(child.getId()) && child.getParentIds().indexOf("," + extId + ",") == -1)) {
		        if (child.getParentId().equals(atLeftTree.getId())) {
		            atLeftTree.getChildren().add(getChildOfTree(child, atLeftTreeList, extId));
		        }
		    }
		}
		return atLeftTree;
	}



}